package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.mbg.bas.entity.BasFloor;
import com.kingint.mbg.bas.entity.BasRoom;
import com.kingint.mbg.bas.mapper.BasFloorMapper;
import com.kingint.mbg.bas.mapper.BasRoomMapper;
import com.kingint.mbg.dto.floor.BasFloorReturn;
import com.kingint.web.admin.base.service.BasFloorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 楼层表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-21
 */
@Service
public class BasFloorServiceImpl extends ServiceImpl<BasFloorMapper, BasFloor> implements BasFloorService {

    @Resource
    private BasFloorMapper basFloorMapper;
    @Autowired
    private UmsMemberService umsMemberService;
    @Resource
    private BasRoomMapper roomMapper;

    @Override
    public IPage<BasFloor> pagelist(Page<BasFloor> page, BasFloor basFloor) {
        QueryWrapper<BasFloor> queryWrapper = new QueryWrapper<>();
        Integer userHotel = umsMemberService.getUserHotel();
        queryWrapper.eq("hotel_id",userHotel);
        if (!StringUtils.isEmpty(basFloor.getFloorName())){
            queryWrapper.eq("floor_name",basFloor.getFloorName());
        }
        IPage<BasFloor> basFloorIPage = basFloorMapper.selectPage(page, queryWrapper);
        return basFloorIPage;
    }

    @Override
    public List<BasFloorReturn> getFloorRoomList() {
        QueryWrapper<BasFloor> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",umsMemberService.getUserHotel());
        List<BasFloor> basFloors = basFloorMapper.selectList(wrapper);


        List<BasFloorReturn> list=new ArrayList<>();
        if(!CollectionUtils.isEmpty(basFloors)){
            for (BasFloor info:basFloors) {
                BasFloorReturn aReturn = new BasFloorReturn();
                BeanUtils.copyProperties(info,aReturn);
                QueryWrapper<BasRoom> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("floor_id",info.getId());
                List<BasRoom> basRooms = roomMapper.selectList(wrapper1);
                aReturn.setRoomList(basRooms);
                list.add(aReturn);
            }
        }
        return list;
    }
}
